import cookies from './util.cookies'
import db from './util.db'
import log from './util.log'
import tool from './util.tool'

const util = {
  tool,
  cookies,
  db,
  log
}

/**
 * @description 更新标题
 * @param {String} title 标题
 */
util.title = function (titleText) {
  const processTitle = process.env.VUE_APP_TITLE || 'D2Admin'
  window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ''}`
}

/**
 * @description 打开新页面
 * @param {String} url 地址
 */
util.open = function (url) {
  var a = document.createElement('a')
  a.setAttribute('href', url)
  a.setAttribute('target', '_blank')
  a.setAttribute('id', 'd2admin-link-temp')
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(document.getElementById('d2admin-link-temp'))
}

util.timeStampToDate = function (nS) {  
  var date = new Date(nS * 1000)
  var year = date.getFullYear()
  var month = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
  var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
}

util.dateToTimeStamp = function (val) {
  var date = new Date(val)
  return date.getTime() / 1000
}

util.getUploadUrl = function (path) {
  // 取配置apihost
  return process.env.VUE_APP_API + path
}

util.getUploadHeader = function () {
  return { 
    'Authorization': 'Bearer ' + cookies.get('token'),
    'X-Token': cookies.get('token')
  }
}

util.getDownloadTemplate = function (type) {
  return process.env.VUE_APP_API + 'template/' + type + '-template.txt'
}

export default util
